﻿
var Globle_TrObjBgColor = "";//记录交替行的颜色并处理HOVER效果与点击变色的冲突


//绑定所有的按钮信息列表
function BindAllList() {
    $.ajax({
        url: "ButtonInfo.ashx?t=" + new Date().getDate().toString(),
        dataType: "json",
        type: "post",
        data: { stype: "getall" },
        success: function (data) {
            if (data.state) {
                var temp_TrObjs = "";
                for (var rindex = 0; rindex < data.rows.length; rindex++) {
                    temp_TrObjs += "<tr bid=\"" + data.rows[rindex].ID + "\" bguid=\"" + data.rows[rindex].ButtonID + "\">";
                    temp_TrObjs += "<td class=\"top_table_td1\">" + data.rows[rindex].ID + "</td>";
                    temp_TrObjs += "<td class=\"top_table_td2\">" + decodeURIComponent(data.rows[rindex].Name) + "</td>";
                    temp_TrObjs += "<td class=\"top_table_td3\">" + decodeURIComponent(data.rows[rindex].Description) + "</td>";
                    temp_TrObjs += "</tr>";
                }

                $("#tb_ButtonList").html(temp_TrObjs);
                
                Button_SetBgColor();
            }
            else {
                $("#tb_ButtonList").html("<tr bid=\"0\" bguid=\"0\"><td colspan=\"3\">没有任何按钮被添加</td></tr>");
            }
        },
        error: function (i) {
            alert("出现异常，请刷新再试！");
        }
    });
}

//设置交替行颜色
function Button_SetBgColor() {
    $("#tb_ButtonList").find("tr:even").css("background-color", "white");
    $("#tb_ButtonList").find("tr:odd").css("background-color", "#F1EFE2");
}

//节点所属按钮的隔行变色效果
function Button_NoteSetBgColor() {
    $("#tb_ButtonsByNote").find("tr:even").css("background-color", "white");
    $("#tb_ButtonsByNote").find("tr:odd").css("background-color", "#F1EFE2");
}

//设置鼠标移过的特效
function Button_SetHover() {    
    $("#tb_ButtonList").find("tr").live({
        mouseenter: function () {
            Globle_TrObjBgColor = $(this).css("background-color");
            $(this).css("background-color", "#C3E3FF");
        },
        mouseleave: function () {
            $(this).css("background-color", Globle_TrObjBgColor);
        }
    });
}

//添加按钮信息的点击事件
function AddButtonInfo() {
    var temp_name = $("#txt_buttonname").val();
    var temp_Description = $("#txt_buttonDescription").val();
    var temp_HTML = $("#ttr_buttonHTML").val();
    var temp_Javascript = $("#ttr_buttonJavascript").val();

    var temp_showmessage = "";
    if ($.trim(temp_name) == "") {
        temp_showmessage += "请添加按钮名称\n";
    }

    if ($.trim(temp_Description) == "") {
        temp_showmessage += "请添加按钮描述\n";
    }

    if ($.trim(temp_HTML) == "") {
        temp_showmessage += "请添加按钮HTML代码\n";
    }

    if (temp_showmessage != "") {
        alert(temp_showmessage);
        return;
    }

    $.ajax({
        url: "ButtonInfo.ashx?t=" + new Date().getDate().toString(),
        dataType: "json",
        type: "post",
        data: { stype: "addbtn", bname: encodeURIComponent(temp_name), bdesc: encodeURIComponent(temp_Description), bhtml: encodeURIComponent(temp_HTML), bjava: encodeURIComponent(temp_Javascript) },
        success: function (data) {
            if (data.state) {
                $("#lb_buttonID").text(data.bid);
                $("#hd_buttonGuid").val(data.bguid);
                var tempTrObj = "<tr bid=\"" + data.bid + "\" bguid=\"" + data.bguid + "\">";

                tempTrObj += "<td class=\"top_table_td1\">" + data.bid + "</td>";
                tempTrObj += "<td class=\"top_table_td2\">" + temp_name + "</td>";
                tempTrObj += "<td class=\"top_table_td3\">" + temp_Description + "</td>";                

                if ($("#tb_ButtonList").find("tr").length <= 1 && $("#tb_ButtonList").find("tr").eq(0).attr("bid") == "0") {
                    $("#tb_ButtonList").html(tempTrObj)
                }
                else {
                    $("#tb_ButtonList").prepend(tempTrObj);
                }

                Button_SetBgColor();

                $("#tb_ButtonList").find("tr").eq(0).css("background-color", "#B0C4DE");

                alert("添加成功！");
            }
            else {
                alert("添加失败，请刷新后重试！");
            }
        },
        error: function (i) {
            alert("出现异常，请刷新再试！");
        }
    });
}

//按钮列表行的点击事件
function ButtonList_TrClick(TrObj) {
    if (TrObj.attr("bid") == "0") {
        return;
    }
    $.ajax({
        url: "ButtonInfo.ashx?t=" + new Date().getDate().toString(),
        dataType: "json",
        type: "post",
        data: { stype: "getsingle", bid: TrObj.attr("bid"), bguid: TrObj.attr("bguid") },
        success: function (data) {
            if (data.state) {
                $("#txt_buttonname").val(decodeURIComponent(data.name));
                $("#txt_buttonDescription").val(decodeURIComponent(data.desc));
                $("#ttr_buttonHTML").val(decodeURIComponent(data.html));
                $("#ttr_buttonJavascript").val(decodeURIComponent(data.java));
                $("#lb_buttonID").text(data.bid);
                $("#hd_buttonGuid").val(data.bguid);

                Button_SetBgColor();

                TrObj.css("background-color", "#B0C4DE");

                Globle_TrObjBgColor = "#B0C4DE";
            }
            else {
            }
        },
        error: function (ex, i) {
            //alert(i);
        }
    });
}

//重置的方法
function BindClearAllInput() {
    $("#txt_buttonname").val("");
    $("#txt_buttonDescription").val("");
    $("#ttr_buttonHTML").val("");
    $("#ttr_buttonJavascript").val("");
    $("#lb_buttonID").text("");
    $("#hd_buttonGuid").val("");
}

//保存按钮信息的方法
function Button_SaveInfo() {
    var temp_bID = $("#lb_buttonID").text();
    var temp_bGuid = $("#hd_buttonGuid").val();
    var temp_name = $("#txt_buttonname").val();
    var temp_Description = $("#txt_buttonDescription").val();
    var temp_HTML = $("#ttr_buttonHTML").val();
    var temp_Javascript = $("#ttr_buttonJavascript").val();

    var temp_showmessage = "";

    if (temp_bID == "" || temp_bGuid == "") {
        temp_showmessage = "请选择一条记录！";
    }

    if (temp_showmessage != "") {
        alert(temp_showmessage);
        return;
    }

    if ($.trim(temp_name) == "") {
        temp_showmessage += "请填写按钮名称\n";
    }

    if ($.trim(temp_Description) == "") {
        temp_showmessage += "请填写按钮描述\n";
    }

    if ($.trim(temp_HTML) == "") {
        temp_showmessage += "请填写按钮HTML代码\n";
    }

    if (temp_showmessage != "") {
        alert(temp_showmessage);
        return;
    }
    $.ajax({
        url: "ButtonInfo.ashx?t=" + new Date().getDate().toString(),
        dataType: "json",
        type: "post",
        data: {
            stype: "savebtn",
            bname: encodeURIComponent(temp_name),
            bdesc: encodeURIComponent(temp_Description),
            bhtml: encodeURIComponent(temp_HTML),
            bjava: encodeURIComponent(temp_Javascript),
            bid: temp_bID,
            bguid: temp_bGuid
        },
        success: function (data) {
            if (data.state) {
                $("#tb_ButtonList").find("tr").each(function (i) {
                    if ($(this).attr("bguid") == temp_bGuid) {
                        $(this).find("td").eq(1).text(temp_name);
                        $(this).find("td").eq(2).text(temp_Description);
                    }
                });
                $("#tb_ButtonsByNote").find("tr").each(function (i) {
                    if ($(this).attr("bguid") == temp_bGuid) {
                        $(this).find("td").eq(0).text(temp_name);
                    }
                })
                alert("保存成功！");
            }
            else {
                alert("保存失败，请刷新后重试！");
            }
        },
        error: function (i) {
            alert("出现异常，请刷新再试！");
        }
    });
}

//删除一条记录的方法
function Button_DeleteInfo() {
    var temp_bID = $("#lb_buttonID").text();
    var temp_bGuid = $("#hd_buttonGuid").val();

    var temp_showmessage = "";

    if (temp_bID == "" || temp_bGuid == "") {
        temp_showmessage = "请选择一条记录！";
    }

    if (temp_showmessage != "") {
        alert(temp_showmessage)
        return;
    }

    $.ajax({
        url: "ButtonInfo.ashx?t=" + new Date().getDate().toString(),
        dataType: "json",
        type: "post",
        data: { stype: "deletesingle", bid: temp_bID, bguid: temp_bGuid },
        success: function (data) {
            if (data.state) {
                $("#tb_ButtonList").find("tr").each(function (i) {
                    if ($(this).attr("bguid") == temp_bGuid) {
                        $(this).remove();
                    }
                });
                BindClearAllInput();

                Button_SetBgColor();

                alert("删除记录成功！");
            }
            else {
            }
        },
        error: function (ex, i) {
            //alert(i);
        }
    });
}

//查询按钮信息的方法
function Button_SearchWords(keyWords) {
    if ($.trim(keyWords) == "") {
        alert("请填写关键词");
        return;
    }

    $.ajax({
        url: "ButtonInfo.ashx?t=" + new Date().getDate().toString(),
        dataType: "json",
        type: "post",
        data: { stype: "getsearch", keywds: keyWords },
        success: function (data) {
            if (data.state) {
                var temp_TrObjs = "";
                for (var rindex = 0; rindex < data.rows.length; rindex++) {
                    temp_TrObjs += "<tr bid=\"" + data.rows[rindex].ID + "\" bguid=\"" + data.rows[rindex].ButtonID + "\">";
                    temp_TrObjs += "<td class=\"top_table_td1\">" + data.rows[rindex].ID + "</td>";
                    temp_TrObjs += "<td class=\"top_table_td2\">" + decodeURIComponent(data.rows[rindex].Name) + "</td>";
                    temp_TrObjs += "<td class=\"top_table_td3\">" + decodeURIComponent(data.rows[rindex].Description) + "</td>";
                    temp_TrObjs += "</tr>";
                }

                $("#tb_ButtonList").html(temp_TrObjs);

                Button_SetBgColor();
            }
            else {
                $("#tb_ButtonList").html("<tr><td colspan=\"3\">与关键词 " + keyWords + " 相关的记录数为0</td></tr>");
            }
        },
        error: function (i) {
            alert("出现异常，请刷新再试！");
        }
    });
}

//将按钮添加到节点的方法（右边列表）
function Button_AddToNote() {
    var temp_bID = $("#lb_buttonID").text();
    var temp_bGuid = $("#hd_buttonGuid").val();
    var temp_name = "";
    var temp_IsAdded = false;//是否已添加

    var temp_showmessage = "";

    if (temp_bID == "" || temp_bGuid == "") {
        temp_showmessage = "请选择一条记录！";
        return;
    }

    $("#tb_ButtonsByNote").find("tr").each(function (i) {
        if ($(this).attr("bguid") == temp_bGuid) {
            temp_IsAdded = true;
        }
    });

    if (temp_IsAdded) {
        alert("该按钮已添加！");
        return;
    }

    $("#tb_ButtonList").find("tr").each(function (i) {
        if ($(this).attr("bguid") == temp_bGuid) {
            temp_name = $(this).find("td").eq(1).text();
        }
    });

    var temp_RightTrObj = "<tr bid=\"" + temp_bID + "\" bguid=\"" + temp_bGuid + "\">";
    temp_RightTrObj += "<td class=\"top_table_td4\">" + temp_name + "</td>";
    temp_RightTrObj += "<td class=\"top_table_td5\"><a href=\"#\">移除</a></td>";
    temp_RightTrObj += "<td class=\"top_table_td5\"><img src='../images/ico6.gif' onclick='move(this,-1)' title='向上'/></td>";
    temp_RightTrObj += "<td class=\"top_table_td5\"><img src='../images/ico5.gif' onclick='move(this,1)' title='向下'/></td>";
    temp_RightTrObj += "</tr>";

    $("#tb_ButtonsByNote").append(temp_RightTrObj);

    Button_NoteSetBgColor();
}
//保存菜单的按钮事件
function Button_SaveToNote() { 
    var TreeID=$("#lb_TreeID").html();
    if (TreeID == "") {
        alert("请选择菜单节点！");
        return;
    }
    var btnIDs = "";
    $("#tb_ButtonsByNote").find("tr").each(function (i) {
        btnIDs += (i == 0) ? "" : "@";
        btnIDs += $(this).attr("bid");
    });
    $.ajax({
        url: "ButtonInfo.ashx?t=" + new Date().getDate().toString(),
        dataType: "text",
        type: "post",
        data: { stype: "SaveMenuBtn", TreeID: TreeID, btnIDs: btnIDs },
        success: function (data) {
            var dataJson = window.eval("(" + data + ")");
            if (dataJson.state) {
                alert('保存成功！');
            } else
                alert('保存失败！');
        },
        error: function (i) {
            alert("出现异常，请刷新再试！");
        }
    });
}
//绑定文本框的回车事件
function Input_SearchoK() {
    $("#txt_keywords").keydown(function (e) {
        if (e.keyCode == 13) {
            $("#btn_Search").click();
        }
    });  
}

//给按钮绑定事件
function BindButtonEvent() {
    //添加按钮的点击事件
    $("#btn_AddButton").bind("click", function () {
        AddButtonInfo();
    });

    //按钮列表行的点击事件
    $("#tb_ButtonList").find("tr").live("click", function () {
        ButtonList_TrClick($(this));
    });
    $("#tb_ButtonsByNote").find("tr").live("click", function () {
        ButtonList_TrClick($(this));
        Button_NoteSetBgColor();
    });
    //重置按钮的点击事件
    $("#btn_ResetButoon").bind("click", function () {
        BindClearAllInput();
    });

    //保存按钮的点击事件
    $("#btn_SaveButton").bind("click", function () {
        Button_SaveInfo();
    });

    //删除按钮的点击事件
    $("#btn_DeleteButton").bind("click", function () {
        Button_DeleteInfo();
    });

    //显示全部按钮点击事件
    $("#btn_ShowAll").bind("click", function () {
        BindAllList();
    });

    //按关键词查询的点击事件
    $("#btn_Search").bind("click", function () {
        Button_SearchWords($("#txt_keywords").val());
    });

    //添加按钮到节点按钮列表的点击事件
    $("#btn_AddByNote").bind("click", function () {
        Button_AddToNote();
    });

    //节点所属(右边列表)按钮移除点击事件
    $("#tb_ButtonsByNote").find("a").live("click", function () {
        $(this).parents("tr").remove();
        Button_NoteSetBgColor();
    });
    //菜单按钮保存事件
    $("#btn_SaveByNote").bind("click", function () {
        Button_SaveToNote();
    });
    //搜索文本框的回车事件
    Input_SearchoK();
}
//菜单树加载
function MenuBind() {
    $.ajax({
        url: "ButtonInfo.ashx?t=" + new Date().getDate().toString(),
        dataType: "text",
        type: "post",
        data: { stype: "getMenu" },
        success: function (data) {
            var dataJson = window.eval("(" + data + ")");
            if (dataJson.state) {
                var setting = { 
                    callback: {
                        onClick: BindButtonList
                    }
                };
                var zNodes = dataJson.rows;
                $.fn.zTree.init($("#ul_Tree"), setting, zNodes);
            }
        },
        error: function (i) {
            alert("出现异常，请刷新再试！");
        }
    });
}
function move(obj, index) {
    var Other=null;
    if (index == -1) {
        Other = $(obj).parents("tr").prev("tr");
        if (Other.attr("bguid")!=$(obj).attr("bguid")) {
            Other.before($(obj).parents("tr").clone(true));
            $(obj).parents("tr").remove(); //删除当前的
        }
    }
    if (index == 1) {
        Other = $(obj).parents("tr").next("tr");
        if (Other.attr("bguid") != $(obj).attr("bguid")) {
            Other.append($(obj).parents("tr").clone(true));
            $(obj).parents("tr").remove(); //删除当前的
        }
    }  
}

function BindButtonList(event, treeId, treeNode, clickFlag) {
    $("#lb_TreeID").html(treeNode.OID);
    $.ajax({
        url: "ButtonInfo.ashx?t=" + new Date().getDate().toString(),
        dataType: "text",
        type: "post",
        data: { stype: "getMenuButtonList", TreeID: treeNode.OID },
        success: function (data) {
            var dataJson = window.eval("(" + data + ")");
            if (dataJson.state) {
                var btnStr = "";
                for (var i = 0; i < dataJson.rows.length; i++) {
                    var item = dataJson.rows[i];
                    btnStr += "<tr bid=\"" + item.ID + "\" bguid=\"" +item.ButtonID + "\">";
                    btnStr += "<td class=\"top_table_td4\">" + item.Name + "</td>";
                    btnStr += "<td class=\"top_table_td5\"><a href=\"#\">移除</a></td>";
                    btnStr += "<td class=\"top_table_td5\"><img src='../images/ico6.gif' onclick='move(this,-1)' title='向上'/></td>";
                    btnStr += "<td class=\"top_table_td5\"><img src='../images/ico5.gif' onclick='move(this,1)' title='向下'/></td>";
                    btnStr += "</tr>";
                }
                $("#tb_ButtonsByNote").html(btnStr);
                Button_NoteSetBgColor();
            }
        },
        error: function (i) {
            alert("出现异常，请刷新再试！");
        }
    });
}	
//页面加载时的方法
$(document).ready(function () {
    //绑定菜单数据
    MenuBind();
    //绑定按钮点击事件
    BindButtonEvent();
    //加载所有的按钮
    BindAllList();
    //设定鼠标滑过的特效
    Button_SetHover();
});